package models;

import play.*;
import play.data.binding.As;
import play.db.jpa.*;
import play.modules.search.Field;
import play.modules.search.Indexed;

import javax.persistence.*;

import java.util.*;

@Indexed
@Entity
public class NewsItem extends Model {
    
	@Field
	public String title;
	
	@Field
	public String content;
    
    @As("yyyy-MM-dd")
    public Date postedAt;
    
    @Field
    @ManyToOne
    public User postedBy;
    
    @ManyToOne
    public Course course;
    
    public NewsItem(User postedBy, String title, String content) {
    	this.postedBy = postedBy;
    	this.title = title;
    	this.content = content;
    	this.postedAt = new Date();
    	create();
    }
    
    public NewsItem(User postedBy, String title, String content, Course course) {
    	this.postedBy = postedBy;
    	this.title = title;
    	this.content = content;
    	this.postedAt = new Date();
    	this.course = course;
    	create();
    }
    
    public static List<NewsItem> getMainNews() {
    	return NewsItem.find("course = null order by postedAt desc").fetch();
    }
}
